Page({
  data: {
    isLike: true,
    // banner
    imgUrls: [
      "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
    ],
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    duration: 1000, //  滑动动画时长1s

    // 商品详情介绍
    detailImg: [
      "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
      // "https://img.alicdn.com/imgextra/i1/2627785630/O1CN01JuLXte1rSaRu5MiS5_!!0-item_pic.jpg_430x430q90.jpg",
    ],
    particulars: [],

    //商品数据
    particularss: [],
    //商品所在的分类id
    class_id: '1'
  },
  onLoad:function(options) {
    console.log(options);
    
    const id = options;
    // console.log(id);
    this.setData({
      paid: id
    })
  
    
   var that = this;
    wx.request({
          url: 'http://127.0.0.1/bnstudy/public/index.php/api/v1/course_detail?id=' + that.data.paid.id,
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data.data[0].cate_id);
            that.setData({
              particulars: res.data.data,
              class_id: res.data.data[0].cate_id
            })
          } 
        })
     
  },

  //预览图片
  previewImage: function (e) {
    var current = e.target.dataset.src;

    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imgUrls // 需要预览的图片http链接列表  
    })
  },
  // 收藏
  addLike() {
    this.setData({
      isLike: !this.data.isLike
    });
  },
  // 跳到购物车
  toCar() {
    wx.switchTab({
      url: '/pages/cart/cart'
    })
  },
  // 立即购买
  immeBuy() {
    // console.log(res);
    var that = this;
    // console.log(that.data.class_id);
    
    wx.request({
      url: 'http://127.0.0.1/bnstudy/public/index.php/api/v1/number?class_id=' + that.data.class_id,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data.data[0][0].number);
        // that.setData({
        //   particulars: res.data.data
        // })
wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000
    });

      } 
    })
   


    
  },
  //加入购物车
  addCar() {
    console.log('加入购物车');
    console.log(this.data.particulars[0].id);
    
    let cart = wx.getStorageSync('cart')||[];
    let index = cart.findIndex(v=>v.id===this.data.particulars[0].id);
     
    if(index === -1) {
      this.data.particulars[0].sold_num = 1;
      cart.push(this.data.particulars[0])
     
    }else {
      cart[index].sold_num++
    }
    wx.setStorageSync('cart', cart)
    wx.showToast({
      title: '加入成功',
    })
  }
})